
/**
 * 标准字典对照参数初始化
 */
var SdsDicMatch = {
    id: "SdsDicMatchTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsDicMatch.initColumn = function () {
    return [[
        {title: '对照类型', field: 'compareType', width: 80, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                if(value=="K"){
                    return "关键字";
                }
                if(value=="C"){
                    return "医嘱通用名";
                }
            },
            editor:{
                type:'combobox',
                options:{required:true,valueField:'code',textField:'desc',editable:false,
                    data:
                        [
                            {'code':'K','desc':"关键字"},
                            {'code':'C','desc':"医嘱通用名"}
                        ]}
            }},
        {title: '对照内容', field: 'compareContent', width: 150, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
            }},
        {title: '过滤方式', field: 'filterType', width: 80, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                if(value=="I"){
                    return "纳入";
                }
                if(value=="O"){
                    return "排除";
                }
            },
            editor:{
                type:'combobox',
                options:{required:true,valueField:'code',textField:'desc',editable:false,
                    data:
                        [
                            {'code':'I','desc':"纳入"},
                            {'code':'O','desc':"排除"}
                        ]}
            }},
        {title: '备注', field: 'resume', width: 100, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'text',
                options:{required:true}
            }},
    ]];
};

/**
 * 检查是否选中
 */
SdsDicMatch.check = function () {
    var selected = $('#' + this.id).datagrid('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsDicMatch.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加新一行表格
 */
SdsDicMatch.add = function () {
    var rowNum=$('#' + SdsDicMatch.id).datagrid("getRows").length;
    //添加一行
    $('#' + SdsDicMatch.id).datagrid('appendRow',{
        compareType: '',
        compareContent: '',
        filterType: '',
        resume: ''
    });
    var rowIndex= rowNum;
    $('#' + SdsDicMatch.id).datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
};

/**
 * 保存数据到库表
 */
SdsDicMatch.save = function () {
    //保存完毕后，同步数据
    var operation_save = function(){
        SdsDicMatch.table.acceptChanges();//调用datagrid自带的方法，确认其修改 然后保存至数据库
        var dicId = $("#dicId").val();
        var rows =  $('#' + SdsDicMatch.id).datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            row.compareContent = encodeURIComponent(row.compareContent);
            if (row != undefined && row != {}) {
                row.dicId = dicId;
                if (row.id != undefined) {
                    //update Row
                    delete row.updateDate;
                    delete row.updateUser;
                    Sys.ajax(Sys.backendPath + "/sds/sdsstanddic/updateMatch", row, function (data) {
                        Sys.success("修改成功!");
                    },function(data){
                        Sys.error("修改失败!" + data.responseJSON.message + "!");
                    }, "json", false, "修改项目保存至库中..");
                } else {
                    //add Row
                    Sys.ajax(Sys.backendPath + "/sds/sdsstanddic/addMatch", row, function (data) {
                        Sys.success("添加成功!");
                    },function(data){
                        Sys.error("添加失败!" + data.responseJSON.message + "!");
                    }, "json", false, "新增项目保存至库中..");
                }
            }
        }
    };
    Sys.confirm("是否保存表格数据至数据库?",operation_save);
};

/**
 * 删除行同时删除数据库表数据
 */
SdsDicMatch.del = function () {
    if (SdsDicMatch.check()) {
        var operation = function(){
            if(SdsDicMatch.seItem.id!=null&&SdsDicMatch.seItem.id!=""){
                var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddic/deleteMatch", function (data) {
                    Sys.success("删除成功!");
                    SdsDicMatch.table.refresh();
                }, function (data) {
                    Sys.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("sdsDicMatchId",SdsDicMatch.seItem.id);
                ajax.start();
            }else{
                SdsDicMatch.table.delRow();
            }
        };
        Sys.confirm("是否删除该行数据?",operation);
    }
};
/**
 * 查询
 */
SdsDicMatch.search = function () {
    var queryData = {};
    queryData['dicId'] = $("#dicId").val();
    SdsDicMatch.table.refresh({queryParams: queryData});
};


$(function () {
    var dicId = $("#dicId").val();
    var defaultColunms = SdsDicMatch.initColumn();
    //var table = new HISUITable(SdsDicMatch.id, Sys.backendPath + "/sds/sdsstanddic/listMatch", defaultColunms);
    var table = new HISUITable(SdsDicMatch.id, Sys.backendPath + '/sds/sdsstanddic/listMatch/' + dicId + "/", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-30);
    table.setQueryParams({"dicId":0});
    table.set("dicId", 0);
    SdsDicMatch.table = table.init();

});

